// pages/component/carddepots/carddepot/carddepot.js
var GlobalData = require('../../../../utils/GlobalData.js');
var dataManager = require('../../../../utils/dataManager.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    kind: [],
    showkind: {},
    totalCount: 0,
    cards: [],
    gamgKindAndCards: [],
    /**判断点击的bool */
    arrBool: [],
    pic: ["top"],
    takeCard: false,
    selecteCard: {},
    keytext: "",
    cdkey: [],
    showCDK: false,
    hidden: true,
    isShowIntegral:false,
    isshowiphonex:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
 * 生命周期函数--监听页面显示
 */
  onShow: function () {
    if (GlobalData.isshowiphone) {
      this.setData({
        isshowiphonex: true
      })
    } else {
      this.setData({
        isshowiphonex: false
      })
    }
    if (app.getConfig().isShowIntegral) {
      this.setData({
        isShowIntegral: app.getConfig().isShowIntegral
      })
    }
    this.setData({
      hidden: true
    })
    dataManager.removeBtnObserver("carddepot")

    if (GlobalData.DepotCard.length && !GlobalData.isSave) {
      this.setData({
        kind: GlobalData.DepotKind,
        // showkind: GlobalData.clone(GlobalData.DepotKind[0])
      })
      this.getTotalCard()
      this.onCardComplete();
      GlobalData.isSave = false;
    } else {

      dataManager.rigisterObserver("depotkind", this.onKindComplete, this);
      dataManager.getDepotKind();
    }
  },
  onKindComplete: function () {
    this.setData({
      kind: GlobalData.DepotKind,
      // showkind: GlobalData.clone(GlobalData.DepotKind[0])
    })

    dataManager.rigisterObserver("depotcard", this.onCardComplete, this);
    dataManager.getDepotCard();

  },
  onCardComplete: function () {
    var cardarr = GlobalData.DepotCard;
    this.setData({
      cards: GlobalData.DepotCard
    })
    this.getTotalCard();
    // /**房卡分类 */
    var gameKindTemp = []
    var arrB = []
    for (let i = 0; i < this.data.kind.length; i++) {
      gameKindTemp[i] = [];
      arrB[i] = true;
      for (let j = 0; j < this.data.cards.length; j++) {
        if (this.data.kind[i].gameid == this.data.cards[j].game_id) {
          if (this.data.cards[j].totalcount) {
            gameKindTemp[i].push(GlobalData.clone(this.data.cards[j]));
          }
        }
      }
    }

    let kindTemp = this.data.kind
    for (let i = 0; i < gameKindTemp.length; i++) {
      if (gameKindTemp[i].length == 0) {
        gameKindTemp.splice(i, 1);
        kindTemp.splice(i, 1);
        arrB.splice(i, 1)
        --i
      }
    }
    var picTemp = []
    for (let k = 0; k < arrB.length; k++) {
      if (arrB[k]) {
        picTemp[k] = "top"
      } else {
        picTemp[k] = "right"
      }
    }
    this.setData({
      gamgKindAndCards: gameKindTemp,
      arrBool: arrB,
      kind: kindTemp,
      pic: picTemp
    })

    if (!this.data.gamgKindAndCards.length) {
      this.setData({
        totalCount: 0
      })
    }
  },
  /**计算卡库总数量 */
  getTotalCard: function () {
    var count = 0;
    for (let i = 0; i < GlobalData.DepotCard.length; i++) {
      count += GlobalData.DepotCard[i]["totalcount"];
    }
    this.setData({
      totalCount: count
    })
  },
  /**展开或收起 */
  onPickUp: function (e) {
    let index = e.currentTarget.dataset.id;
    var istrue = this.data.arrBool[index]
    if (istrue) {
      this.data.arrBool[index] = false;
      this.data.pic[index] = "right";
    } else {
      this.data.arrBool[index] = true;
      this.data.pic[index] = "top";
    }
    this.setData({
      arrBool: this.data.arrBool,
      pic: this.data.pic
    })
  },
  /**选取某种卡 */
  onSelecte: function (e) {
    var card = e.currentTarget.dataset.id;
    card["count"] = 0
    this.setData({
      takeCard: true,
      selecteCard: card
    })

  },
  /**关闭取卡弹框0 */
  onClose: function () {
    this.setData({
      takeCard: false,
    })
  },
  onAdd: function () {

    if (this.data.selecteCard.count < this.data.selecteCard.totalcount) {
      this.data.selecteCard.count++;
    }
    this.setData({
      selecteCard: this.data.selecteCard
    })
  },
  onReduce: function () {

    if (this.data.selecteCard.count > 0) {
      this.data.selecteCard.count--;
    }
    this.setData({
      selecteCard: this.data.selecteCard
    })
  },


  //取卡
  onGetCardCDK: function () {
    if (GlobalData.isquKa)
      return
    if (!this.data.selecteCard.count)
      return;
    GlobalData.isquKa = true;
    dataManager.OnRequestPost({ method: "takecard", "token": app.getToken(), "id": this.data.selecteCard.id, "count": this.data.selecteCard.count }, this.onProcessCDK);

  },
  /**处理卡密数据 */
  onProcessCDK: function (data) {
    //关闭弹窗
    this.setData({
      takeCard: false,
      keytext: ""
    })
    for (let i = 0; i < data.length; i++) {
      this.data.cdkey[i] = data[i];
      this.data.keytext = this.data.keytext + data[i];
      if (i < data.length - 1)
        this.data.keytext = this.data.keytext + '\r\n';
    }
    this.setData({
      cdkey: this.data.cdkey,
      keytext: this.data.keytext
    })
    dataManager.rigisterObserver("depotcard", this.onCardComplete, this);
    dataManager.getDepotCard();
    //释放取卡记录
    GlobalData.GetCardRecord.splice(0, GlobalData.GetCardRecord.length);
    GlobalData.GetCardRecordother[0] = 1
    // GetcardrecordComponent.index = 1;
    this.setData({
      showCDK: true,
    })

  },
  /**复制卡密 */
  onCopyCDK: function () {
    let that = this
    this.setData({
      showCDK: false,
    })

    wx.setClipboardData({
      data: that.data.keytext,
      success() {
        wx.getClipboardData({
          success(res) {
            app.show("已复制成功。")
            GlobalData.isquKa = false;
          }
        })
      },
      fail(res) {
        console.error(res)
      }
    })
  },


  showTxt: function (e) {
    var str = ""
    if (GlobalData.homeData.businessname)
      str = GlobalData.homeData.businessname
    var msg = "添加商务微信" + GlobalData.homeData.businessname + "，即可开通推广功能"
    wx.showModal({
      title: "提示",
      content: msg,
      showCancel: true,
      confirmText: "确认复制",
      success: function (res) {
        if (res.confirm) {
          wx.setClipboardData({
            data: str,
            success() {
              wx.getClipboardData({
                success(res) {
                  // app.show("已复制成功。")
                }
              })
            },
            fail(res) {
              console.error(res)
            }
          })
        }
      }
    })
  },
  goMall() {
    dataManager.UseNavigateTo( "/pages/integralmall/integralmall",true)
  },
  /**前往推广 */
  goPromotion: function () {
    if (!GlobalData.homeData.isopendistribution) {
      app.show("该地区暂未开启推广功能");
      return;
    }
    else if (GlobalData.homeData.isopendistribution && (GlobalData.homeData.isdefaultlevels && !GlobalData.homeData.isdefaultshare)) {
      // this.showTxt("提示", "添加商务微信" + GlobalData.homeData.businessname + "，即可开通推广功能", "确认复制微信号");
      this.showTxt();
      return;
    }
    else {
      // wx.redirectTo({
      //   url: '../../promotions/promotion/promotion'
      // })
      this.setData({
        hidden: false
      })
      dataManager.UseNavigateTo('../../promotions/promotion/promotion', true);
    }

  },
  /**前往主页 */
  goHome: function () {
    // wx.redirectTo({
    //   url: '../../homes/home/home'
    // })
    this.setData({
      hidden: false
    })
    dataManager.UseNavigateTo('../../homes/home/home', true);
  },
  /**前往取卡记录 */
  goGetCardRecord: function () {
    // if (GlobalData.isjiLu)
    //   return
    // GlobalData.isjiLu = true;
    if (!this.data.hidden) {
      return;
    }
    this.setData({
      hidden: false
    })

    dataManager.OnRequestPost({ method: "getagentcdkeyloglist", "token": app.getToken(), "pageindex": 1, "pagesize": 20 }, this.onProcessGetCardRecord, false, this.onError);
  },
  onError: function () {
    this.setData({
      hidden: true
    })
  },
  onProcessGetCardRecord: function (data) {
    for (let i = 0; i < data.length; i++)
      GlobalData.GetCardRecord[i] = data[i];
    // if (dataManager.isClick("getcardrecord")) {
    //   return
    // } else {
    //   dataManager.btnObserver("getcardrecord")
    this.setData({
      hidden: true
    })
    dataManager.UseNavigateTo('../../carddepots/getcardrecord/getcardrecord', false);
    // }

  },
  /**前往购买房卡 "pages/component/homes/purchase/buycard/buycard", */
  goBuyCard: function () {
    // if (dataManager.isClick("buycard")) {
    //   return
    // } else {
    //   dataManager.btnObserver("buycard")
    this.setData({
      hidden: false
    })
    dataManager.UseNavigateTo('../../homes/purchase/buycard/buycard', false);
    // }

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})